<template>
  <a-modal
    :title="(model&&model.id) ? '编辑': '新增'"
    :width="900"
    :height="360"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleOK"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" style="display: none;">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="类型名称">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入类型名称' }] }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

  <script>
  import pick from 'lodash.pick'
  import locale from 'ant-design-vue/es/date-picker/locale/zh_CN'
  import WangEditor from '@/components/Editor/WangEditor.vue'
  // 表单字段
  const fields = ['name', 'id', 'createTime', 'updateTime']
  export default {
    components: { WangEditor },
    props: {
      visible: {
        type: Boolean,
        required: true
      },
      loading: {
        type: Boolean,
        default: () => false
      },
      model: {
        type: Object,
        default: () => null
      }
    },
    data () {
      this.formLayout = {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        }
      }
      return {
        form: this.$form.createForm(this),
        fileList: [],
        // imageUrl: '',
        uploadloading: false,
        locale: locale
      }
    },
    created () {
      // 防止表单未注册
      fields.forEach(v => this.form.getFieldDecorator(v))

      // 当 model 发生改变时，为表单设置值
      this.$watch('model', () => {
        if (this.model) {
          this.form.setFieldsValue(pick(this.model, fields))
        }
      })
    },
    methods: {
      handleOK () {
        this.$emit('ok')
      }
    }
  }
  </script>
  <style scoped>
  .avatar-uploader > .ant-upload {
    width: 128px;
    height: 128px;
  }
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
  </style>
